//
//  TwoViewController.m
//  反应能力测试游戏
//
//  Created by 王国栋 on 15/9/2.
//  Copyright (c) 2015年 xiaobai. All rights reserved.
//

#import "FirstViewController.h"
#import "SecViewController.h"
@interface FirstViewController ()
@property (nonatomic,strong)NSTimer *timer;
@property (nonatomic,strong)UIButton *btn_enter;
@property (nonatomic,strong)UIButton *btn_help;
@property (nonatomic,strong)UIButton *btn_showgrade;
@end

@implementation FirstViewController
/**
 *背景图是一个动画，界面上面有两个按钮,查看目前游戏的排名，帮助，进入游戏
 */
- (void)viewDidLoad {
    [super viewDidLoad];
    
    //1.设置定时器,控制按钮的旋转
    self.timer=[NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(rota) userInfo:nil repeats:YES];
    
    //2.设置背景图片
    UIImageView *imaView=[[UIImageView alloc]initWithFrame:[UIScreen mainScreen].bounds];
    [imaView setImage:[UIImage imageNamed:@"bg1"]];
    [self.view addSubview:imaView];
    
    
    //3.0创建开始按钮 245 222 179
    self.btn_enter=[[UIButton alloc]initWithFrame:CGRectMake(40, 120, 60, 60)];
    [self.btn_enter setBackgroundColor:[UIColor colorWithRed:245/255 green:222/255 blue:179/255 alpha:0]];
    self.btn_enter.layer.cornerRadius=30;
    [self.btn_enter setBackgroundImage:[UIImage imageNamed:@"enter"] forState:UIControlStateNormal];
    [self.btn_enter addTarget:self action:@selector(enter_game) forControlEvents:UIControlEventTouchUpInside];
    self.btn_enter.layer.masksToBounds=YES;//把图片的尖角去掉
    self.btn_enter.layer.borderColor=[[UIColor blackColor]CGColor];//设置边的颜色
    self.btn_enter.layer.borderWidth=2.0;
    [imaView addSubview:self.btn_enter];
    
    //3.1创建开始按钮提示标签
    UILabel *Lbe_enter=[[UILabel alloc]initWithFrame:CGRectMake(45,190,60, 40)];
    Lbe_enter.text=@"开始";
    [Lbe_enter setFont:[UIFont systemFontOfSize:28 weight:10]];
    [imaView addSubview:Lbe_enter];

    //4.0创建帮助按钮
    self.btn_help=[[UIButton alloc]initWithFrame:CGRectMake(140, 120, 60, 60)];
    [self.btn_help setBackgroundColor:[UIColor colorWithRed:245/255 green:222/255 blue:179/255 alpha:0]];
    self.btn_help.layer.cornerRadius=30;
    [self.btn_help setBackgroundImage:[UIImage imageNamed:@"help"] forState:UIControlStateNormal];
    [self.btn_help addTarget:self action:@selector(show_help) forControlEvents:UIControlEventTouchUpInside];
    self.btn_help.layer.masksToBounds=YES;//把图片的尖角去掉
    self.btn_help.layer.borderColor=[[UIColor blackColor]CGColor];//设置边的颜色
    self.btn_help.layer.borderWidth=2.0;
    [imaView addSubview:_btn_help];
    
    //4.1创建帮助标签
    UILabel *Lbe_help=[[UILabel alloc]initWithFrame:CGRectMake(145,190,60, 40)];
    Lbe_help.text=@"帮助";
    [Lbe_help setFont:[UIFont systemFontOfSize:28 weight:10]];
    [imaView addSubview:Lbe_help];

    //5.0创建查看成绩的按钮
    self.btn_showgrade=[[UIButton alloc]initWithFrame:CGRectMake(240, 120, 60, 60)];
    [self.btn_showgrade setBackgroundColor:[UIColor colorWithRed:245/255 green:222/255 blue:179/255 alpha:0]];
    self.btn_showgrade.layer.cornerRadius=30;
    [self.btn_showgrade setBackgroundImage:[UIImage imageNamed:@"wind"] forState:UIControlStateNormal];
    [self.btn_showgrade addTarget:self action:@selector(show_grade) forControlEvents:UIControlEventTouchUpInside];
    self.btn_showgrade.layer.masksToBounds=YES;//把图片的尖角去掉
    self.btn_showgrade.layer.borderColor=[[UIColor blackColor]CGColor];//设置边的颜色
    self.btn_showgrade.layer.borderWidth=2.0;
    [imaView addSubview:self.btn_showgrade];
    //5.1创建成绩标签
    UILabel *Lbe_greade=[[UILabel alloc]initWithFrame:CGRectMake(245,190,60, 40)];
    Lbe_greade.text=@"记录";
    [Lbe_greade setFont:[UIFont systemFontOfSize:28 weight:10]];
    [imaView addSubview:Lbe_greade];
    //6.0 创建退出按钮
    UIButton *btn_exit=[[UIButton alloc]initWithFrame:CGRectMake(130, 410, 60, 60)];
    [btn_exit setBackgroundColor:[UIColor colorWithRed:245/255 green:222/255 blue:179/255 alpha:0]];
    btn_exit.layer.cornerRadius=30;
    [btn_exit setBackgroundImage:[UIImage imageNamed:@"exit"] forState:UIControlStateNormal];
    [btn_exit addTarget:self action:@selector(exit_game) forControlEvents:UIControlEventTouchUpInside];
    btn_exit.layer.masksToBounds=YES;//把图片的尖角去掉
    btn_exit.layer.borderColor=[[UIColor cyanColor]CGColor];//设置边的颜色
    btn_exit.layer.borderWidth=2.0;
    [imaView addSubview:btn_exit];
    
    UILabel *Lbe_exit=[[UILabel alloc]initWithFrame:CGRectMake(135,480,60, 40)];
    Lbe_exit.text=@"退出";
    [Lbe_exit setFont:[UIFont systemFontOfSize:28 weight:10]];
    [imaView addSubview:Lbe_exit];
    [self.view addSubview:Lbe_exit];
    //开始游戏按钮监听按键
    
    [imaView setUserInteractionEnabled:YES];//必须有这句话，没有上面按钮无法响应事件
    
}
//按钮的旋转
-(void)rota
{
    self.btn_enter.transform=CGAffineTransformRotate(self.btn_enter.transform, M_PI/18);
    self.btn_help.transform=CGAffineTransformRotate(self.btn_help.transform, M_PI/18);
    self.btn_showgrade.transform=CGAffineTransformRotate(self.btn_showgrade.transform, M_PI/18);
}
//进入游戏
-(void)enter_game
{
    SecViewController *vc=[[SecViewController alloc]init];
    [self.navigationController pushViewController:vc animated:YES];
}
//查看帮助
-(void)show_help
{
    NSString *str=@"请按照数字的顺序依次点击按钮，所用的时间越短成绩越好,选择中等模式创造记录可以记录成绩呦";
    UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"游戏规则" message:str delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
    [alert show];
}
-(void)show_grade
{
    NSString *str=[[NSBundle mainBundle]pathForResource:@"score.plist" ofType:nil];
    NSMutableDictionary *dict=[NSMutableDictionary dictionaryWithContentsOfFile:str];
    if ([[dict allKeys][0]isEqualToString:@"name"]) {
        
        NSString *str=@"恭喜你，你是第一个游戏者,赶快游戏创造记录吧";
        UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"惊喜" message:str delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
    }
    else
    {
        NSString *mes=[NSString stringWithFormat:@"当前纪录保持着是%@，成绩是：%@秒",\
                       [dict allKeys][0],[dict allValues][0]];
        UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"纪录" message:mes delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        
        [alert show];
    }
}
-(void)exit_game
{
    exit(0);
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
